import { Card, message, Row, Col, Space } from 'antd';
import { history } from 'umi';
import ProForm, {
  ProFormRadio,
  ProFormText,
  ProFormUploadButton,
  ProFormTextArea,
} from '@ant-design/pro-form';
import { useRequest } from 'umi';
import type { FC } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import { fakeSubmitForm, queryDetail, update } from './service';
import Address from './components/Address';
import { queryDictList } from '@/services/ant-design-pro/api';
import RichText from '@/components/RichText';

const BasicForm: FC<Record<string, any>> = () => {
  const { run } = useRequest(fakeSubmitForm, {
    manual: true,
    onSuccess: () => {
      message.success('提交成功');
      history.push('/hospital/hospital-list');
    },
  });

  // const { data, loading } = useRequest(() => {
  //   if (history.location.query.id) {
  //     return queryDetail(history.location.query);
  //   }
  // });

  const onFinish = async (values: Record<string, any>) => {
    console.log('values11', values);
    try {
      if (values.upload[0].response) {
        values.logo = values.upload[0].response.data.url;
      };
      if (values.headPicUpload[0].response) {
        values.headPic = values.headPicUpload[0].response.data.url;
        delete values.headPicUpload;
      }
      // if (values.dz.province) {
      //   values.provinceName = values.dz.province.label;
      //   values.provinceCode = values.dz.province.value;
      // }
      // if (values.province) {
      //   values.provinceName = values.province.label;
      //   values.provinceCode = values.province.value;
      // }
      // if (values.dz.city) {
      //   values.cityName = values.dz.city.label;
      //   values.cityCode = values.dz.city.value;
      // }
      // if (values.city) {
      //   values.cityName = values.city.label;
      //   values.cityCode = values.city.value;
      // }

      // if (values.dz.district) {
      //   values.areaName = values.dz.district.label;
      //   values.areaCode = values.dz.district.value;
      // }
      // if (values.district) {
      //   values.areaName = values.district.label;
      //   values.areaCode = values.district.value;
      // }

      // values.addressDetail = values.dz.detailed;
    }
    catch (error) {
      console.log('error', error);
    }

    if (history.location.query.id) {
      values.id = history.location.query.id;
      try {
        const response = await update(values);
        if (response.code == 200) {
          message.success('修改成功');
          history.push('/hospital/hospital-list');
        } else {
          message.error('修改失败!');
        }
      } catch (error) {
        console.log(error);
      }
    } else {
      run(values);
    }
  };

  const formItemLayout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 14 },
  };

  return (
    <PageContainer content="">
      <Card bordered={false}>
        <ProForm
          hideRequiredMark
          style={{ margin: 'auto', marginTop: 8 }}
          name="basic"
          layout="horizontal"
          // initialValues={data}
          onFinish={onFinish}
          {...formItemLayout}
          request={async () => {
            if (history.location.query.id) {
              return queryDetail(history.location.query).then(({ data }) => {
                // let diseaseIds = [];
                // for (var i = 0; i < data.diseaseRelationList.length; i++) {
                //   diseaseIds.push(data.diseaseRelationList[i].diseaseId);
                // }
                // data.diseaseIds = diseaseIds;
                // data.addUserType = data.addUserType + '';
                console.log('data', data);
                // const dz = {};
                // dz.address = data.addressDetail;
                // const province = {};
                // province.label = data.provinceName;
                // province.value = data.provinceCode;
                // dz.province = province;

                // const city = {};
                // city.label = data.cityName;
                // city.value = data.cityCode;
                // dz.city = city;

                // const district = {};
                // district.label = data.areaName;
                // district.value = data.areaCode;
                // dz.district = district;
                // dz.detailed = data.addressDetail;
                // data.dz = dz;
                data.addressDetail= data.provinceName+ data.cityName+ data.areaName+data.addressDetail
                if (data.logo) {
                  data.upload = [
                    {
                      uid: '1',
                      name: 'icon',
                      status: 'done',
                      url: 'http://oss.yizuoyiyou.com.cn' + data.logo,
                    },
                  ];
                }
                if (data.headPic) {
                  data.headPicUpload = [
                    {
                      uid: '1',
                      name: 'icon',
                      status: 'done',
                      url: 'http://oss.yizuoyiyou.com.cn' + data.headPic,
                    },
                  ];
                }
                data.businessStatus = data.businessStatus + '';
                return data;
              });
            } else {
              return {};
            }
          }}
          submitter={{
            render: (props, doms) => {
              return (
                <Row>
                  <Col span={14} offset={4}>
                    <Space>{doms}</Space>
                  </Col>
                </Row>
              );
            },
          }}
        >
          <ProFormText
            width="md"
            label="医院名称"
            name="name"
            rules={[
              {
                required: true,
                message: '请输入医院名称',
              },
            ]}
            placeholder="请输入医院名称"
          />
          <ProFormUploadButton
            name="upload"
            label="医院logo"
            max={1}
            fieldProps={{
              name: 'file',
              listType: 'picture-card',
            }}
            action="/api/file/upload"
          />
          <ProFormText
            width="md"
            label="医院全称"
            name="fullName"
            rules={[
              {
                required: true,
                message: '请输入医院全称',
              },
            ]}
            placeholder="请输入医院全称"
          />
          {/* <ProForm.Item
            name="dz"
            label="医院地址"
            rules={[
              {
                required: true,
                message: '请输入医院地址',
              },
            ]}
            valuePropName="checked"
          >
            <Address />
          </ProForm.Item> */}
          <ProFormTextArea name="addressDetail" label="医院地址" placeholder="请输入医院地址"
            rules={[
              {
                required: true,
                message: '请输入医院地址',
              },
            ]} />
          <ProFormRadio.Group
            request={async () => {
              const params = { dictCode: 'grade' };
              return queryDictList(params).then(({ data }) => {
                return data.map((item) => {
                  return {
                    label: item.itemName,
                    value: item.itemName,
                  };
                });
              });
            }}
            label="医院等级"
            name="grade"
          />
          <ProFormUploadButton
            name="headPicUpload"
            label="医院门头图"
            max={1}
            fieldProps={{
              name: 'file',
              listType: 'picture-card',
            }}
            action="/api/file/upload"
          //action="/api/file/oss"
          />
          <ProFormRadio.Group
            request={async () => {
              const params = { dictCode: 'operationNature' };
              return queryDictList(params).then(({ data }) => {
                return data.map((item) => {
                  return {
                    label: item.itemName,
                    value: item.itemName,
                  };
                });
              });
            }}
            label="运营性质"
            name="operationNature"
          />
          <ProFormRadio.Group
            request={async () => {
              const params = { dictCode: 'professionalNature' };
              return queryDictList(params).then(({ data }) => {
                return data.map((item) => {
                  return {
                    label: item.itemName,
                    value: item.itemName,
                  };
                });
              });
            }}
            label="专业性质"
            name="professionalNature"
          />

          <ProFormTextArea name="excellentDept" label="特色科室" placeholder="请输入特色科室" />
          <ProFormTextArea name="intro" label="医院简介" placeholder="请输入医院简介" />
          <ProFormTextArea name="visitInfo" label="门诊时间" placeholder="请输入门诊时间" />
          <ProForm.Item label="挂号攻略" name="registration">
            <RichText />
          </ProForm.Item>
          <ProFormRadio.Group
            options={[
              {
                value: '0',
                label: '营业',
              },
              {
                value: '1',
                label: '歇业',
              },
            ]}
            label="营业状态"
            name="businessStatus"
          />
        </ProForm>
      </Card>
    </PageContainer>
  );
};

export default BasicForm;
